package uy.com.tsi2G7.superbet.integration.betfair.rss;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class RSSFeedParser {
	static final String TITLE = "title";
	static final String DESCRIPTION = "description";
	static final String CHANNEL = "channel";
	static final String LANGUAGE = "language";
	static final String COPYRIGHT = "copyright";
	static final String LINK = "link";
	static final String AUTHOR = "author";
	static final String ITEM = "item";
	static final String PUB_DATE = "pubDate";
	static final String GUID = "guid";

	final URL url;

	public RSSFeedParser(String feedUrl) {
		try {
			this.url = new URL(feedUrl);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	public Feed readFeed() {
		Feed feed = null;
		try {
			
			 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	         DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	         Document doc = docBuilder.parse (read());
			
	      // normalize text representation
	            doc.getDocumentElement ().normalize ();
	            System.out.println ("Root element of the doc is " + 
	                 doc.getDocumentElement().getNodeName());
	            
	             
	            NodeList channel = doc.getDocumentElement().getFirstChild().getChildNodes();
	            feed = new Feed();
	            feed.setEntries(new ArrayList<FeedMessage>());
				for (int i = 0; i < channel.getLength(); i++) {
	            		if (channel.item(i).getNodeName() == (TITLE)) {
	            			feed.setTitle(channel.item(i).getFirstChild().getTextContent());
	            		}else if (channel.item(i).getNodeName() == (DESCRIPTION)) {
							feed.setDescription(channel.item(i).getFirstChild().getTextContent());
	            		}else if (channel.item(i).getNodeName() == (LINK)) {
							feed.setLink(channel.item(i).getFirstChild().getTextContent());
	            		}else if (channel.item(i).getNodeName() == (PUB_DATE)) {
							feed.setPubDate(channel.item(i).getFirstChild().getTextContent());
	        			}else if (channel.item(i).getNodeName() == (ITEM)) {
							FeedMessage currentMessage=new FeedMessage();
							for (int j = 0; j < channel.item(i).getChildNodes().getLength(); j++){
								if (channel.item(i).getChildNodes().item(j).getNodeName() == (TITLE)) {
			            			currentMessage.setTitle(channel.item(i).getChildNodes().item(j).getFirstChild().getTextContent());
								}else if (channel.item(i).getChildNodes().item(j).getNodeName() == (DESCRIPTION)) {
									currentMessage.setDescription(channel.item(i).getChildNodes().item(j).getFirstChild().getTextContent());
								}else if (channel.item(i).getChildNodes().item(j).getNodeName() == (LINK)) {
									currentMessage.setLink(channel.item(i).getChildNodes().item(j).getFirstChild().getTextContent());
								}
							 }
							feed.getEntries().add(currentMessage);
						}
	            
	            }
	    } catch (Exception e) {
			throw new RuntimeException(e);
		}
		    return feed; 
	}

	private InputStream read() {
		try {
			return url.openStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}